package com.missilike.api.controller;

import com.alibaba.fastjson.JSON;
import com.missilike.api.domain.response.InternalResponse;
import com.missilike.core.domain.WechatGetUserInfo;
import com.missilike.core.result.CommonResult;
import com.missilike.core.service.impl.WechatService;
import com.missilike.core.utils.ResultUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController
@RequestMapping("/api/wechat")
public class WechatController {

    private WechatService wechatService;


    public WechatController(WechatService wechatService) {
        this.wechatService = wechatService;
    }

    @ApiOperation(value = "getUserInfo", notes = "获取微信用户信息")
    @RequestMapping(value = "/getUserInfo", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public CommonResult getUserInfo(@RequestParam String code) {
        log.info("Start to execute /wechat/getUserInfo  RequestParam ={}", code);
         WechatGetUserInfo wechatGetUserInfo= wechatService.getUserInfo(code);
        log.info("Completed execute /wechat/getUserInfo responese={}",wechatGetUserInfo);
        return ResultUtils.ok(wechatGetUserInfo);
    }

    @ApiOperation(value = "getAuthorizationTicket", notes = "获取微信ticket信息")
    @RequestMapping(value = "/getAuthorizationTicket", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public CommonResult getAuthorizationTicket() {
        log.info("Start to execute /wechat/getAuthorizationTicket ");
        String ticket= wechatService.getTicket();
        log.info("Completed execute /wechat/getUserInfo responese={}", JSON.toJSON(ticket));
        return ResultUtils.ok(ticket);
    }

    @ApiOperation(value = "sendWechatBeforehandPay", notes = "微信预支付")
    @RequestMapping(value = "/sendWechatBeforehandPay", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public CommonResult  sendWechatBeforehandPay() {
        log.info("Start to execute /wechat/sendWechatBeforehandPay");
        wechatService.sendWechatBeforehandPay();
        log.info("Completed execute /wechat/sendWechatBeforehandPay responese={}");
        return ResultUtils.ok();
    }

    @ApiOperation(value = "responseWechatPayResult", notes = "通知微信支付完成")
    @RequestMapping(value = "/responseWechatPayResult", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public CommonResult  responseWechatPayResult() {
        log.info("Start to execute /wechat/responseWechatPayResult ");
        wechatService.sendWechatPayResult();
        log.info("Completed execute /wechat/responseWechatPayResult responese={}");
        return ResultUtils.ok();
    }
}
